package cn.uniquetech.util;

import java.util.List;

public class StrUtil {

	public static String initCap(String str) {
		char[] ch = str.toCharArray();
		if (ch[0] >= 'a' && ch[0] <= 'z') {
			ch[0] = (char) (ch[0] - 32);
		}
		return new String(ch);
	}

	// 首字母变大
	public static String captureName(String name) {
		char[] cs = name.toCharArray();
		cs[0] -= 32;
		return String.valueOf(cs);
	}

	public static String getSimpleClassName(String tableName, boolean deleteTailChar) {
		/*
		 * String clazz = StrUtil.initCap(tableName); if (deleteTailChar) {
		 * clazz = clazz.substring(0, clazz.length() - 1); } return clazz;
		 */
		return tableName.toLowerCase();
	}

	public static void getAllAttrs(StringBuffer sb, List<String> colNames, List<String> colTypes) {

		for (int i = 0; i < colNames.size(); i++) {
			sb.append("\tprivate " + sqlType2JavaType(colTypes.get(i)) + " " + colNames.get(i) + ";\r\n");
		}
	}

	public static String parseEntity(String packageName, String tableName, List<String> colNames, List<String> colTypes, int[] colSizes) {
		String servicePackage = packageName + ".vo";
		StringBuffer sb = new StringBuffer();
		sb.append("package " + servicePackage + ";\r\n");
		sb.append("\r\n");
		sb.append("import cn.uniquetech.entity.base.BaseEntity;\r\n");

		String clazz = StrUtil.initCap(tableName);
		clazz = clazz.substring(0, clazz.length() - 1);
		sb.append("@SuppressWarnings(\"serial\")" + "\r\n");
		sb.append("public class " + clazz + "Entity extends BaseEntity" + " {\r\n");
		getAllAttrs(sb, colNames, colTypes);
		sb.append("}\r\n");

		FileUtil.writeFile(servicePackage, clazz + "Entity.java", sb.toString());
		return sb.toString();
	}

	// service 生成接口
	public static String parseServiceInterface(String packageName, String tableName) {
		String servicePackage = packageName + ".service";
		StringBuffer sb = new StringBuffer();
		sb.append("package " + servicePackage + ";\r\n");
		String clazz = StrUtil.getSimpleClassName(tableName, true);
		sb.append("import cn.uniquetech.service.base.IBaseService;\r\n");
		sb.append("\r\n\r\npublic interface I" + clazz + "Service extends IBaseService" + "{\r\n\r\n");
		sb.append("}\r\n");
		FileUtil.writeFile(servicePackage, "I" + clazz + "Service.java", sb.toString());
		return sb.toString();
	}

	// service 生成实现类
	public static String parseServiceClass(String packageName, String tableName) {
		String servicePackage = packageName + ".service.impl";
		StringBuffer sb = new StringBuffer();
		sb.append("package " + servicePackage + ";\r\n\r\n");
		String clazz = StrUtil.getSimpleClassName(tableName, true);
		sb.append("import org.springframework.stereotype.Service;\r\n");
		sb.append("import cn.uniquetech.service.I" + clazz + "Service;");
		sb.append("\r\n\r\n");
		sb.append("@Service");
		sb.append("\r\n");
		sb.append("public class " + clazz + "ServiceImpl extends BaseServiceImpl implements I" + clazz + "Service" + " {\r\n\r\n");
		// sb.append("private I" + clazz + "Service " + clazz.toLowerCase() +
		// "Service;\r\n");
		sb.append("public " + clazz + "ServiceImpl() {\r\n");
		sb.append("\t\tsuper();\r\n");
		sb.append("\t\tthis.mapper = \"" + clazz + "Mapper\";\r\n");
		sb.append("\t}\r\n");
		sb.append("}\r\n");
		FileUtil.writeFile(servicePackage, clazz + "ServiceImpl.java", sb.toString());
		return sb.toString();
	}

	// 生成 controller
	public static String parseControllerClass(String packageName, String tableName) {
		String servicePackage = packageName + ".controller";
		String clazz = StrUtil.getSimpleClassName(tableName, true);
		StringBuffer sb = new StringBuffer();
		sb.append("package " + servicePackage + ";\r\n\r\n");
		sb.append("import org.springframework.beans.factory.annotation.Autowired;\r\n");
		sb.append("import org.springframework.stereotype.Controller;\r\n");
		sb.append("import org.springframework.web.bind.annotation.RequestMapping;\r\n");
		sb.append("import cn.uniquetech.controller.base.BaseController;\r\n");
		sb.append("import cn.uniquetech.entity." + clazz + "Entity;\r\n");
		sb.append("import cn.uniquetech.service.I" + clazz + "Service;\r\n");
		sb.append("\r\n");
		sb.append("@Controller");
		sb.append("\r\n");
		sb.append("@RequestMapping(value = \"*/" + clazz.toLowerCase() + "\")");
		sb.append("\r\n");
		sb.append("public class " + clazz + "Controller extends BaseController<" + clazz + "Entity>" + "{\r\n\r\n");
		sb.append("private I" + clazz + "Service " + clazz.toLowerCase() + "Service;" + "\r\n");
		sb.append("@Autowired\r\n");
		sb.append("public " + clazz + "Controller(I" + clazz + "Service " + clazz.toLowerCase() + "Service) {\r\n");
		sb.append("\t\tsuper();\r\n");
		sb.append("\t\t" + "this." + clazz.toLowerCase() + "Service = " + clazz.toLowerCase() + "Service;" + "\r\n");
		sb.append("\t\t" + "this.baseService = " + clazz.toLowerCase() + "Service;" + "\r\n");
		sb.append("\t\t" + "this.ctrlModel = \"" + clazz.toLowerCase() + "\";" + "\r\n");
		sb.append("\t\t" + "this.ctrlName = \"" + clazz.toLowerCase() + "\";\r\n");
		sb.append("\t}\r\n");
		sb.append("}\r\n");
		FileUtil.writeFile(servicePackage, clazz + "Controller.java", sb.toString());
		return sb.toString();
	}

	public static String sqlType2JavaType(String sqlType) {

		if (sqlType.equalsIgnoreCase("bit")) {
			return "boolean";
		} else if (sqlType.equalsIgnoreCase("tinyint")) {
			return "byte";
		} else if (sqlType.equalsIgnoreCase("smallint")) {
			return "short";
		} else if (sqlType.equalsIgnoreCase("int")) {
			return "int";
		} else if (sqlType.equalsIgnoreCase("bigint")) {
			return "long";
		} else if (sqlType.equalsIgnoreCase("float")) {
			return "float";
		} else if (sqlType.equalsIgnoreCase("decimal") || sqlType.equalsIgnoreCase("numeric") || sqlType.equalsIgnoreCase("real") || sqlType.equalsIgnoreCase("money") || sqlType.equalsIgnoreCase("smallmoney")) {
			return "double";
		} else if (sqlType.equalsIgnoreCase("varchar") || sqlType.equalsIgnoreCase("char") || sqlType.equalsIgnoreCase("nvarchar") || sqlType.equalsIgnoreCase("nchar") || sqlType.equalsIgnoreCase("text")) {
			return "String";
		} else if (sqlType.equalsIgnoreCase("datetime")) {
			return "Date";
		} else if (sqlType.equalsIgnoreCase("image")) {
			return "Blod";
		}

		return null;
	}

	public static String sc(String packageName, String tableName, List<String> colNames, List<String> colTypes, int[] colSizes) {
		String result = "";
		result += GenXMLUtil.getXmlStr(tableName, packageName, colNames);
		result += parseEntity(packageName, tableName, colNames, colTypes, colSizes);
		result += parseServiceClass(packageName, tableName);
		result += parseControllerClass(packageName, tableName);
		result += parseServiceInterface(packageName, tableName);
		return result;
	}

}
